/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Checkbox;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.OnRangeSelectedListener;
import com.prolificinteractive.materialcalendarview.sample.decorators.RangeDayDecorator;
import com.prolificinteractive.materialcalendarview.sample.tools.ToastsUtils;

import org.threeten.bp.format.DateTimeFormatter;

import java.util.List;

/**
 * @since 2021-07-12
 */
public class SelectionModesAbility extends BaseAbility implements OnDateSelectedListener, OnRangeSelectedListener, AbsButton.CheckedStateChangedListener {
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, d MMM yyyy");

    private MaterialCalendarView single;
    private MaterialCalendarView multi;
    private MaterialCalendarView range;
    private MaterialCalendarView none;
    private RangeDayDecorator decorator;
    private Checkbox calendarMode;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        single = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendar_view_single);
        multi = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendar_view_multi);
        range = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendar_view_range);
        none = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendar_view_none);
        single.setSelectionColor(COLOR_ACCENT);
        multi.setSelectionColor(COLOR_ACCENT);
        range.setSelectionColor(COLOR_ACCENT);
        none.setSelectionColor(COLOR_TRA);
        calendarMode = (Checkbox) findComponentById(ResourceTable.Id_calendar_mode);
        calendarMode.setCheckedStateChangedListener(this);
        decorator = new RangeDayDecorator(this);

        single.setOnDateChangedListener(this);
        multi.setOnDateChangedListener(this);
        range.setOnDateChangedListener(this);
        range.setOnRangeSelectedListener(this);
        range.addDecorator(decorator);
        none.setOnDateChangedListener(this);
        toolbarTitle.setText("Calendar Selection Modes");
    }

    @Override
    int contentLayoutRes() {
        return ResourceTable.Layout_ability_selection_modes;
    }

    @Override
    public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
        final String text = selected ? FORMATTER.format(date.getDate()) : "No Selection";
        new ToastsUtils.Builder(getContext())
            .setToastText(text)
            .build().show();
    }

    @Override
    public void onRangeSelected(MaterialCalendarView widget, List<CalendarDay> dates) {
        if (dates.size() > 0) {
            decorator.addFirstAndLast(dates.get(0), dates.get(dates.size() - 1));
            range.invalidateDecorators();
        }
    }

    @Override
    public void onCheckedChanged(AbsButton absButton, boolean checked) {
        final CalendarMode mode = checked ? CalendarMode.WEEKS : CalendarMode.MONTHS;
        single.state().edit().setCalendarDisplayMode(mode).commit();
        multi.state().edit().setCalendarDisplayMode(mode).commit();
        range.state().edit().setCalendarDisplayMode(mode).commit();
        none.state().edit().setCalendarDisplayMode(mode).commit();
    }
}
